package cn.iocoder.yudao.module.biz.controller.admin.risk.equipmentInfo;

import cn.iocoder.yudao.module.biz.controller.admin.risk.basicManage.vo.BizBasicManageExcelVO;
import cn.iocoder.yudao.module.biz.controller.admin.risk.basicManage.vo.BizBasicManageRespVO;
import cn.iocoder.yudao.module.biz.controller.admin.risk.equipmentInfo.vo.*;
import cn.iocoder.yudao.module.biz.controller.admin.risk.taskWork.vo.BizTaskWorkExcelVO;
import cn.iocoder.yudao.module.biz.controller.admin.risk.taskWork.vo.BizTaskWorkImportRespVO;
import cn.iocoder.yudao.module.biz.convert.risk.equipmentInfo.BizEquipmentInfoConvert;
import cn.iocoder.yudao.module.biz.dal.dataobject.risk.equipmentInfo.BizEquipmentInfoDO;
import cn.iocoder.yudao.module.biz.service.risk.equipmentInfo.BizEquipmentInfoService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;
import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.io.BufferedOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import org.springframework.web.multipart.MultipartFile;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;


@Api(tags = "管理后台 - 设备")
@RestController
@RequestMapping("/biz/equipment-info")
@Validated
public class BizEquipmentInfoController {

    @Resource
    private BizEquipmentInfoService equipmentInfoService;

    @PostMapping("/create")
    @ApiOperation("创建设备")
    @PreAuthorize("@ss.hasPermission('biz:equipment-info:create')")
    public CommonResult<String> createEquipmentInfo(@Valid @RequestBody BizEquipmentInfoCreateReqVO createReqVO) {
        return success(equipmentInfoService.createEquipmentInfo(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新设备")
    @PreAuthorize("@ss.hasPermission('biz:equipment-info:update')")
    public CommonResult<Boolean> updateEquipmentInfo(@Valid @RequestBody BizEquipmentInfoUpdateReqVO updateReqVO) {
        equipmentInfoService.updateEquipmentInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除设备")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:equipment-info:delete')")
    public CommonResult<Boolean> deleteEquipmentInfo(@RequestParam("id") String id) {
        equipmentInfoService.deleteEquipmentInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得设备")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:equipment-info:query')")
    public CommonResult<BizEquipmentInfoRespVO> getEquipmentInfo(@RequestParam("id") String id) {
        BizEquipmentInfoDO equipmentInfo = equipmentInfoService.getEquipmentInfo(id);
        return success(BizEquipmentInfoConvert.INSTANCE.convert(equipmentInfo));
    }

    @GetMapping("/list")
    @ApiOperation("获得设备列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('biz:equipment-info:query')")
    public CommonResult<List<BizEquipmentInfoRespVO>> getEquipmentInfoList(@RequestParam("ids") Collection<String> ids) {
        List<BizEquipmentInfoDO> list = equipmentInfoService.getEquipmentInfoList(ids);
        return success(BizEquipmentInfoConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得设备分页")
    @PreAuthorize("@ss.hasPermission('biz:equipment-info:query')")
    public PageResult<BizEquipmentInfoRespVO> getEquipmentInfoPage(@Valid BizEquipmentInfoPageReqVO pageVO) {
        PageResult<BizEquipmentInfoRespVO> result=new PageResult<>();
        List<BizEquipmentInfoRespVO> list = equipmentInfoService.getEquipmentPage(pageVO);
        Integer count= equipmentInfoService.getEquipmentInfoCount(pageVO);
        result.setList(list);
        result.setTotal((long) count);
        return result;
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出设备 Excel")
    @PreAuthorize("@ss.hasPermission('biz:equipment-info:export')")
    @OperateLog(type = EXPORT)
    public void exportEquipmentInfoExcel(@Valid BizEquipmentInfoExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        //List<BizEquipmentInfoDO> list = equipmentInfoService.getEquipmentInfoList(exportReqVO);
        // 导出 Excel
        //List<BizEquipmentInfoExcelVO> datas = BizEquipmentInfoConvert.INSTANCE.convertList02(list);
        List<BizEquipmentInfoExcelVO> datas = equipmentInfoService.getEquipmentInfoForExport(exportReqVO);
        ExcelUtils.write(response, "设备.xls", "数据", BizEquipmentInfoExcelVO.class, datas);
    }

    @PostMapping("/import")
    @ApiOperation("导入设备设施")
    @ApiImplicitParams({@ApiImplicitParam(name = "file", value = "Excel 文件", required = true, dataTypeClass = MultipartFile.class)})
    public CommonResult<BizEquipmentImportRespVO> importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        List<BizEquipmentInfoExcelVO> list = ExcelUtils.read(file, BizEquipmentInfoExcelVO.class);
        return success(equipmentInfoService.importExcel(list));
    }

    @PostMapping(value = "/downEquipmentTemplate")
    @ApiOperation("下载设备设施的模板")
    public HttpServletResponse downEquipmentTemplate(HttpServletResponse response,HttpServletRequest request) {
        try {
            String filename = "设备设施导入模板.xlsx";
            InputStream fis = this.getClass().getClassLoader().getResourceAsStream("template/设备设施导入模板.xls");
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            response.reset();
            // 设置response的Header
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
            //response.addHeader("Content-Length", "" + file.length());
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return response;
    }
}
